package Test;

import javax.swing.*;
import java.awt.*;

public class TextTest {
    private  static void Text(){
        JFrame frame = new JFrame("聊天窗口");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,500);
        frame.setVisible(true);
        frame.setLocation(300,200);
        JTextArea textArea = new JTextArea(12,34);
        JScrollPane scrollPane = new JScrollPane(textArea);
        textArea.setEditable(false);
        JTextField textField = new JTextField(20);
        JButton button = new JButton("发送");
        button.addActionListener(d -> {
            String context = textField.getText();
            if (context != null && !context.trim().equals("")){
                textArea.append("你输入："+context+"\n");
            }else{
                textArea.append("不能输入空数据！");
            }
            textField.setText("");
        });
        JPanel panel = new JPanel();
        JLabel label = new JLabel("聊天信息");
        panel.add(label);
        panel.add(textField);
        panel.add(button);
        frame.add(scrollPane, BorderLayout.PAGE_START);
        frame.add(panel,BorderLayout.PAGE_END);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(TextTest::Text);
    }
}

